package com.yang.wfcuser.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yang.wfcuser.pojo.dto.UserLoginDTO;
import com.yang.wfcuser.pojo.vo.UserListVO;
import com.yang.wfcuser.security.LoginPrincipal;
import com.yang.wfcuser.serive.UserService;
import com.yang.wfcuser.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "1.管理员管理模块")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    //@PreAuthorize("hasAuthority('/list')")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    // public JsonResult list(@ApiIgnore @AuthenticationPrincipal AdminDetails adminDetails) {
    public JsonResult<List<UserListVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询用户列表】的请求，参数：无");
        log.debug("当事人：{}", loginPrincipal);
        log.debug("当事人的ID：{}", loginPrincipal.getId());
        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        List<UserListVO> list = userService.list();
        return JsonResult.ok(list);
    }
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }
}
